import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/components/Login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import(/* webpackChunkName: "about" */ '@/views/Register.vue')
  },
    {
        path: '/mainpage',
        name: 'MainPage',
        component: () => import(/* webpackChunkName: "about" */ '@/views/mainpage/MainPage.vue')
    },
  {
    path: '/mine',
    name: 'Mine',
    component: ()=>import('@/views/mine/Mine.vue')
  },
  {
    path: '/feedback',
    name: 'FeedBack',
    component: ()=>import('@/views/feedback/FeedBack')
  },
  {
    path: '/complaint',
    name: 'Complaint',
    component: ()=>import('@/views/complaint/Complaint')
  },
  {
    path: '/admindispose',
    name: 'admindispose',
    component: ()=>import('@/views/admin/Dispose')
  },
  {
    path: '/admincheck',
    name: 'admincheck',
    component: ()=>import('@/views/admin/Check')
  },
  {
    path: '/chat',
    name: 'Chat',
    component: ()=>import('@/views/chat/Chat')
  },
  {
    path: '/forget',
    name: 'Forget',
    component: ()=>import('@/views/forgetpass/Forget')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//router对象调用
//.beforeEach((进入到哪一个路由,从哪一个路由离开,对应的函数)=>{})
router.beforeEach((to,form,next) =>{
  //如果进入到的路由是登录页或者注册页面，则正常展示
  if(to.path == '/' || to.path == '/register'||to.path=='/forget'){
    next();
  }else if( !(localStorage.getItem('Authorization')) ){
    alert("您还没有登录，请先登录！");
    next('/');     //转入login登录页面，登录成功后会将token存入localStorage
  }else{
    next();
  }
})


export default router
